﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PessModel;
using PessMvc.Models;

namespace PessMvc.Controllers
{
    [Authorize(Roles="Admin, Chair, Editor")]
    [NotNeedToChangePassword]
    public class PaperRuleController : Controller
    {
        private PESSEntities db = new PESSEntities();

        //
        // GET: /PaperRule/

        //public ActionResult Index(int activityid)
        //{
        //    var paperrules = db.PaperRules.Include(p => p.Activity);
        //    return View(paperrules.ToList());
        //}

        //
        // GET: /PaperRule/Details/5

        //public ActionResult Details(int id = 0)
        //{
        //    PaperRule paperrule = db.PaperRules.Find(id);
        //    if (paperrule == null)
        //    {
        //        return HttpNotFound();
        //    }
        //    return View(paperrule);
        //}

        //
        // GET: /PaperRule/Create

        public ActionResult Create(int activityid)
        {
            ViewBag.ActivityID = new SelectList(db.Activities, "ID", "Name", activityid);
            ViewBag.ActID = activityid;
            return View();
        }

        //
        // POST: /PaperRule/Create

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(PaperRule paperrule)
        {
            if (ModelState.IsValid)
            {
                db.PaperRules.Add(paperrule);
                db.SaveChanges();
                return RedirectToAction("Index", "Rule", new { activityid=paperrule.ActivityID});
            }

            ViewBag.ActivityID = new SelectList(db.Activities, "ID", "Name", paperrule.ActivityID);
            ViewBag.ActID = paperrule.ActivityID;
            return View(paperrule);
        }

        //
        // GET: /PaperRule/Edit/5

        public ActionResult Edit(int id = 0)
        {
            PaperRule paperrule = db.PaperRules.Find(id);
            if (paperrule == null)
            {
                return HttpNotFound();
            }
            ViewBag.ActivityID = new SelectList(db.Activities, "ID", "Name", paperrule.ActivityID);
            return View(paperrule);
        }

        //
        // POST: /PaperRule/Edit/5

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(PaperRule paperrule)
        {
            if (ModelState.IsValid)
            {
                db.Entry(paperrule).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index", "Rule", new { activityid = paperrule.ActivityID });
            }
            ViewBag.ActivityID = new SelectList(db.Activities, "ID", "Name", paperrule.ActivityID);
            return View(paperrule);
        }

        //
        // GET: /PaperRule/Delete/5

        public ActionResult Delete(int id = 0)
        {
            PaperRule paperrule = db.PaperRules.Find(id);
            if (paperrule == null)
            {
                return HttpNotFound();
            }
            return View(paperrule);
        }

        //
        // POST: /PaperRule/Delete/5

        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            PaperRule paperrule = db.PaperRules.Find(id);
            db.PaperRules.Remove(paperrule);
            db.SaveChanges();
            return RedirectToAction("Index", "Rule", new { activityid = paperrule.ActivityID });
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}